<template>
	<view class="post-waterfall">
		<view class="waterfall-container">

			<!-- 左列 -->
			<view class="waterfall-column">
				<!-- 添加插槽 -->
				<slot name="header"></slot>
				<view class="post-item" v-for="item in leftList" :key="item.id"
					@longpress.stop="$emit('longPress', item.id)" @click.stop="handleItemClick(item)">
					<!-- <view class="item-overlay" v-if="activeLongPressPostId === item.id"
						@click.stop="$emit('closeOverlay', item.id)">
						<view class="overlay-card">
							<image class="overlay-icon" src="/static/index/local/icon_qqh.png" mode="widthFix"></image>
							<text class="overlay-text">{{ item.longPressTip }}</text>
						</view>
					</view> -->

					<!-- 媒体内容容器 -->
					<view class="media-content">
						<!-- 如果是视频 -->
						<template v-if="isItemVideo(item)">
							<view class="video-wrapper">
								<DomVideoPlayer :src="item.files[0].fileUrl" :autoplay="false" :controls="false"
									:muted="true" object-fit="contain" :show-center-play-btn="false"
									class="post-video-player" />
								<!-- 右上角播放按钮 -->
								<view class="play-icon-overlay">
									<u-icon name="play-right-fill" color="rgba(255, 255, 255, 0.9)" size="10"></u-icon>
								</view>
							</view>
						</template>
						<!-- 如果是图片 -->
						<template v-else>
							<image class="post-image" :src="item.image" mode="aspectFill" :lazy-load="true"
								show-menu-by-longpress></image>
						</template>
					</view>
					<view class="post-info-card">
						<text class="post-title" v-if="item.title">{{ item.title }}</text>
						<text class="post-title" v-else>{{ item.content }}</text>
						<view class="author-info">
							<view class="author-left" @click.stop="handleToPersonalHomepage(item.userId)">
								<u-avatar :src="item.author.avatar" size="30"></u-avatar>
								<text class="author-name">{{ item.author.name }}</text>
							</view>
							<view class="likes-info" @click.stop="handleLike(item)">
								<u-icon :name="item.isLiked ? 'heart-fill' : 'heart'"
									:color="item.isLiked ? '#ff5050' : '#666'" size="14"></u-icon>
								<text class="like-count">{{ item.likeCount | formatLikes }}</text>
							</view>
						</view>
					</view>
				</view>
			</view>
			<!-- 右列 -->
			<view class="waterfall-column">
				<view class="post-item" v-for="item in rightList" :key="item.id"
					@longpress.stop="$emit('longPress', item.id)" @touchend="$emit('touchEnd')"
					@click.stop="handleItemClick(item)">
					<!-- 	<view class="item-overlay" v-if="activeLongPressPostId === item.id"
						@click.stop="$emit('closeOverlay', item.id)">
						<view class="overlay-card">
							<image class="overlay-icon" src="/static/index/local/icon_qqh.png" mode="widthFix"></image>
							<text class="overlay-text">{{ item.longPressTip }}</text>
						</view>
					</view> -->


					<!-- 媒体内容容器 -->
					<view class="media-content">
						<!-- 如果是视频 -->
						<template v-if="isItemVideo(item)">
							<view class="video-wrapper">
								<DomVideoPlayer :src="item.files[0].fileUrl" :autoplay="false" :controls="false"
									:muted="true" object-fit="cover" :show-center-play-btn="false"
									class="post-video-player" />
								<view class="play-icon-overlay">
									<u-icon name="play-right-fill" color="rgba(255, 255, 255, 0.9)" size="10"></u-icon>
								</view>
							</view>
						</template>
						<!-- 如果是图片 -->
						<template v-else>
							<image class="post-image" :src="item.image" mode="aspectFill" :lazy-load="true"
								show-menu-by-longpress></image>
						</template>
					</view>

					<view class="post-info-card">
						<text class="post-title" v-if="item.title">{{ item.title }}</text>
						<text class="post-title" v-else>{{ item.content }}</text>
						<view class="author-info">
							<view class="author-left" @click.stop="handleToPersonalHomepage(item.userId)">
								<u-avatar :src="item.author.avatar" size="30" mode="aspectFill"></u-avatar>
								<view class="author-name">{{ item.author.name }}</view>
							</view>
							<view class="likes-info" @click.stop="handleLike(item)">
								<u-icon :name="item.isLiked ? 'heart-fill' : 'heart'"
									:color="item.isLiked ? '#ff5050' : '#666'" size="14"></u-icon>
								<text class="like-count">{{ item.likeCount | formatLikes }}</text>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>


	</view>
</template>

<script>
	import DomVideoPlayer from '@/components/DomVideoPlayer/DomVideoPlayer.vue';
	export default {
		name: "WaterfallPosts",
		props: {
			leftList: {
				type: Array,
				default: () => []
			},
			rightList: {
				type: Array,
				default: () => []
			},
			activeLongPressPostId: {
				type: [String, Number],
				default: null
			}
		},
		filters: {
			formatLikes(val) {
				if (val >= 10000) return (val / 10000).toFixed(1) + 'w';
				if (val >= 1000) return (val / 1000).toFixed(1) + 'k';
				return val;
			}
		},
		methods: {
			/**
			 * 【关键改动】新增：判断数据项是否为视频
			 */
			isItemVideo(item) {
				return item.files && item.files.length > 0 && item.files[0].isImage === 0;
			},

			/**
			 * 【关键改动】新增：统一的点击处理函数
			 */
			handleItemClick(item) {
				if (this.isItemVideo(item)) {
					// 如果是视频，跳转到视频播放页
					this.navigateToVideoPlayer(item);
				} else {
					// 如果是图片，像以前一样，通知父组件
					this.$emit('itemClick', item);
				}
			},

			/**
			 * 【关键改动】新增：跳转到视频播放页的具体实现
			 */
			navigateToVideoPlayer(item) {
				const videoData = {
					src: item.files[0].fileUrl,
					title: item.content,
					author: {
						name: item.author.name,
						avatar: item.author.avatar
					}
				};

				uni.navigateTo({
					url: `/pages_sub/index/video-player/video-player?data=${encodeURIComponent(JSON.stringify(videoData))}`
				});
			},

			// 
			handleLike(item) {
				this.$emit('like', item)
			},
			handleToPersonalHomepage(userId) {
				uni.navigateTo({
					url: '/pages_sub/user/others/others?userId=' + userId
				})
			},
		}
	};
</script>

<style lang="scss" scoped>
	/* 这里的样式直接从 index.vue 中剪切过来 */
	.post-waterfall {
		padding: 20rpx;
	}

	.waterfall-container {
		display: flex;
		justify-content: space-between;
	}

	.waterfall-column {
		width: 49.3%;
		display: flex;
		flex-direction: column;
	}

	.post-item {
		background-color: #fff;
		border-radius: 12rpx;
		overflow: hidden;
		margin-bottom: 12rpx;
		position: relative;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.08);
		transition: all 0.2s ease-in-out;

		&:active {
			transform: scale(0.97);
			box-shadow: 0 2rpx 15rpx rgba(0, 0, 0, 0.1);
		}

		.post-image {
			width: 100%;
			display: block;
			min-height: 250rpx;
			max-height: 460rpx;
		}

		.post-info-card {
			padding: 15rpx;
		}

		.post-title {
			font-size: 26rpx;
			color: #333;
			line-height: 1.4;
			margin-bottom: 10rpx;
			display: -webkit-box;
			-webkit-line-clamp: 2;
			-webkit-box-orient: vertical;
			overflow: hidden;
			text-overflow: ellipsis;
			font-weight: 700;
		}

		.item-overlay {
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			height: 100%;
			background-color: rgba(0, 0, 0, 0.4);
			display: flex;
			justify-content: center;
			align-items: center;
			z-index: 10;
			padding: 20rpx;
			box-sizing: border-box;
			animation: fadeIn 0.2s ease-in-out;
		}

		.overlay-card {
			background-color: #ffffff;
			border-radius: 20rpx;
			padding: 20rpx 30rpx;
			display: flex;
			flex-direction: column;
			align-items: center;
			max-width: 90%;
			position: relative;
		}

		.overlay-icon {
			width: 70rpx;
			height: 80rpx;
			position: absolute;
			left: 5rpx;
			top: 5rpx;
		}

		.overlay-text {
			font-size: 26rpx;
			color: #333;
			line-height: 1.5;
			text-indent: 2em;
		}

		/* 新增样式 */
		.media-content {
			position: relative;

			.video-wrapper {
				position: relative;

				.post-video-player {
					min-height: 250rpx;
					max-height: 460rpx;
				}
			}

			.play-icon-overlay {
				position: absolute;
				top: 16rpx;
				right: 16rpx;
				width: 40rpx;
				height: 40rpx;
				background-color: rgba(0, 0, 0, 0.4);
				border-radius: 50%;
				display: flex;
				justify-content: center;
				align-items: center;
				z-index: 2;
			}
		}
	}

	.author-info {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-top: 20rpx;

		.author-left {
			display: flex;
			align-items: center;
			flex: 1; // 占据剩余空间
			min-width: 0; // 允许子元素收缩

			.author-name {
				margin-left: 10rpx;
				font-size: 22rpx;
				color: #666;
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
				flex: 1; // 占据剩余空间
				min-width: 0; // 允许文本收缩
			}

			.author-tag {
				display: flex;
				align-items: center;
				border-radius: 16rpx;
				padding: 4rpx 10rpx;
				margin-left: 10rpx; // 与名字保持间距

				.tag-icon {
					width: 24rpx;
					height: 24rpx;
					margin-right: 6rpx;
				}

				.tag-text {
					font-size: 20rpx;
					color: #f81217;
					font-weight: 500;
				}
			}
		}

		.likes-info {
			display: flex;
			align-items: center;
			flex-shrink: 0; // 防止收缩
			margin-left: 20rpx; // 与左侧内容保持间距

			.like-count {
				margin-left: 5rpx;
				font-size: 22rpx;
				color: #666;
			}
		}
	}
</style>